package cn.dansj.handler.interfaces;

import cn.dansj.annotation.InterfaceParam;
import cn.dansj.annotation.MarkAsApi;
import cn.dansj.bean.CurrentThreadLocalUserInfo;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.common.utils.transfer.AssertUtils;
import cn.dansj.handler.InterfaceHandler;

import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class RemoveShortLink extends InterfaceHandler {
    @Override
    @InterfaceParam(require = true, value = "shortlink", comment = "需要删除的短链接")
    public Object execute(Map<String, Object> paramsMap) {
        final String userId = CurrentThreadLocalUserInfo.getCurrentThreadLocalUserInfo().getCacheToken().getSequence();
        final String shortlink = paramsMap.get("shortlink").toString();
        final int delete = jdbcTemplate.update("delete from x_shortlink where user_id=? and short_link=?", userId, shortlink);

        final String key = ShortLinkGenerate.key(shortlink);
        final boolean equals = userId.equals(redisUtils.hget(key, "userId"));
        if (equals) redisUtils.delete(key);
        AssertUtils.state(delete > 0, new InterfaceResponseException(500, "删除失败"));
        return "删除成功";
    }
}
